package cn.com.nes.site.config;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

/**
 * 自定义配置文件
 */
@Slf4j
public class CustomConfigurationProperties {

    private static volatile CustomConfigurationProperties INSTANCE;

    /**
     * 运营平台地址
     */
    private static String OPE_URL;

    /**
     * 厂商平台地址
     */
    private static String MTPF_URL;

    /**
     * 客户平台地址
     */
    private static String CALL_URL;

    /**
     * 易修APP地址
     */
    private static String EASY_REPAIR_URL;

    /**
     * 消息中心地址
     */
    private static String MESSAGE_URL;

    /**
     * 长效管护/易修用户默认密码
     */
    private static String USER_DEFAULT_PASSWORD;

    private CustomConfigurationProperties() {
        log.debug("CustomerConfigurationProperties Constructor....");
        init();
    }

    private static CustomConfigurationProperties getInstance() {
        if (ObjectUtil.isNull(INSTANCE)) {
            synchronized (CustomConfigurationProperties.class) {
                if (ObjectUtil.isNull(INSTANCE)) {
                    INSTANCE = new CustomConfigurationProperties();
                }
            }
        }
        return INSTANCE;
    }

    private void init(){
        log.debug("CustomerConfigurationProperties init method......");
        InputStream in = null;
        Properties prop = new Properties();
        try {
            FileInputStream coreFile = new FileInputStream(
                    new File(System.getProperty("user.dir")).getParent() + "/core_properties/core.properties");
            in = new BufferedInputStream(coreFile);
            // prop.load(in);//直接这么写，如果properties文件中有汉字，则汉字会乱码。因为未设置编码格式。
            prop.load(new InputStreamReader(in, StandardCharsets.UTF_8));
        } catch (IOException e) {
            log.error("自定义配置文件错误：{}", e.getMessage());
            log.error("{}", e);
        } finally {
            initConfig(prop);
            log.debug("init OPE_URL: {}", OPE_URL);
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }

    private static void initConfig(Properties prop)
    {
        if (ObjectUtil.isNull(prop)) {
            return;
        }
        OPE_URL = prop.getProperty("opePath", "https://ope.nes.com.cn:9042");
        MTPF_URL = prop.getProperty("mtpfPath", "https://iot.nesyun.cn:5516");
        CALL_URL = prop.getProperty("callPath", "http://hrcc.nesyun.com:8031");
        EASY_REPAIR_URL = prop.getProperty("easyRepairPath", "https://heat.nes.com.cn:9050");
        USER_DEFAULT_PASSWORD = prop.getProperty("user_default_password", "123456");
        MESSAGE_URL = prop.getProperty("nesMessagePath", "http://8.152.208.41:8031");
    }

    public static String getOpeUrl () {
        getInstance();
        log.debug("getOpeUrl OPE_URL: {}", OPE_URL);
        return OPE_URL;
    }

    public static String getMtpfUrl() {
        getInstance();
        log.debug("getMtpfUrl MTPF_URL: {}", MTPF_URL);
        return MTPF_URL;
    }

    public static String getCallUrl() {
        getInstance();
        log.debug("getCallUrl CALL_URL: {}", CALL_URL);
        return CALL_URL;
    }

    public static String getEasyRepairUrl() {
        getInstance();
        log.debug("getEasyRepairUrl EASY_REPAIR_URL: {}", EASY_REPAIR_URL);
        return EASY_REPAIR_URL;
    }

    public static String getNesMessageUrl() {
        getInstance();
        log.debug("getNesMessageUrl MESSAGE_URL: {}", MESSAGE_URL);
        return MESSAGE_URL;
    }

    public static String getUserDefaultPassword() {
        getInstance();
        log.info("getUserDefaultPassword USER_DEFAULT_PASSWORD: {}", USER_DEFAULT_PASSWORD);
        return USER_DEFAULT_PASSWORD;
    }

}
